import * as React from 'react'
import { Button, NavBar, FlatList, Gird } from 'components'
import { post } from '../../../utils'
import './SelectExpress.less'

export default class SelectExpress extends React.Component<any, any> {

    public render() {
        const { selectExpressData } = this.props
        return (
            <div className="select_express">
                <NavBar
                    title="选择快递"
                    fixed={true}
                />
                <Gird style={{ marginTop: 0, paddingTop: '0.6rem' }}>
                    <FlatList
                        data={selectExpressData}
                        renderItem={(item: any, index: number) => {
                            return <Button className="btn" onClick={this.handleSelect.bind(this, item)} key={index}>{item.name}</Button>
                        }}
                    />
                </Gird>

            </div>
        )
    }

    public componentDidMount() {
        this.getData()
    }

    private getData() {
        post('indexapi/kuaidi100').then((data: any) => {
            const { SET_USER_EXPRESS_DATA } = this.props
            SET_USER_EXPRESS_DATA(data)
        })
    }

    private handleSelect = (data: any) => {
        const { SET_USER_RETURN_DETAIL, history } = this.props
        const returnDetail = { ...this.props.returnDetail }
        returnDetail.express = data
        SET_USER_RETURN_DETAIL(returnDetail)
        history.goBack()
    }
}